#!/usr/bin/env python

from random_generator import TrainingDataGenerator
from random_generator import DocumentGenerator
from naive_network import NaiveBayesianNetworkOpenBayes
from naive_network import NaiveBayesianNetworkCustom

class NaiveNetworkSimulation():
    """
        NaiveNetworkSimulation
    """
    def __init__(self, docsToGenerate = 50000, testsToPerform = 1000, verbose = False):
        self.docsToGenerate = docsToGenerate
        self.testsToPerform = testsToPerform
        self.verbose = verbose

    """
    Performs a naive simulation with a simple toy model
    """
    def perform_simulation(self):
        #generation of random training data
        trainingGenerator = TrainingDataGenerator(self.docsToGenerate)
        trainingSet = trainingGenerator.generate_training_data()

        #network creation
        network = NaiveBayesianNetworkCustom()
        
        if (self.verbose):
            network.set_verbose_mode()
        
        network.build_root('category')
        network.build_topology(trainingGenerator.get_model_attributes())

        #network training, distribution parameters setup
        network.train(trainingSet)

        print ('-------------------------------')
        print ('Training Information:')
        print ('Total documents: ' + str(len(trainingSet['spam']) + len(trainingSet['noSpam'])))
        print ('Total Spam documents: ' + str(len(trainingSet['spam'])))
        print ('Total No Spam documents: ' + str(len(trainingSet['noSpam'])))
        print ('-------------------------------')
        
        docGenerator = DocumentGenerator(trainingGenerator.get_model_attributes())

        spam = 0
        noSpam = 0
        falsePositiveSpam = 0
        falsePositiveNoSpam = 0

        print ('-------------------------------')
        print ('Performing tests')
        print ('-------------------------------')

        for i in range(0,self.testsToPerform):

            doc = docGenerator.generate()
            result = network.classify(doc)
            
            if (result[0] < result[1]):
                #its spam
                spam = spam + 1
                if (not trainingGenerator.is_spam(doc)):
                    falsePositiveSpam = falsePositiveSpam + 1
            else:
                #its not spam
                noSpam = noSpam + 1
                if (trainingGenerator.is_spam(doc)):
                    falsePositiveNoSpam = falsePositiveNoSpam + 1

            if self.verbose:
                
                print ('Document ' + str(doc))

                if(result[0] < result[1]):
                    print 'Spam according to the filter.'
                else:
                    print 'Not spam according to the filter.'

                if (trainingGenerator.is_spam(doc)):
                    print 'Spam according to the model.'
                else:
                    print 'Not spam according to the model.'

                print 'No Spam Probability (score): ' + str(result[0])
                print 'Spam Probability (score): ' + str(result[1])
                print ('-------------------------------')

        print('Detected Spam Total: ' + str(spam))
        print('Detected No Spam Total: ' + str(noSpam))
        print('False Positives Spam: ' + str(falsePositiveSpam))
        print('False Positives No Spam: ' + str(falsePositiveNoSpam))
        print ('-------------------------------')
        print('False Postive Percentage Spam:' + str(float(falsePositiveSpam * 100)/float(spam)))
        print('False Postive Percentage No Spam:' + str(float(falsePositiveNoSpam * 100)/float(noSpam)))
